# -*- coding: utf-8 -*-
"""
@Time ： 2021/11/25 21:26
@Auth ： Mr. William 1052949192
@Company ：特斯汀学院 @testingedu.com.cn
@Function ：12306滑块验证码示例
"""
import time

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

# 使用options，去掉自动化软件控制的标识
option = Options()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_argument('--disable-blink-features=AutomationControlled')
prefs = {}
prefs['credentials_enable_service'] = False
prefs['profile.password_manager_enabled'] = False
option.add_experimental_option('prefs', prefs)  ##关掉密码弹窗

option.add_argument('–disable-gpu') # 谷歌文档提到需要加上这个属性来规避bug
option.add_argument('lang=zh_CN.UTF-8') # 设置默认编码为utf-8

driver = webdriver.Chrome(options=option)
driver.implicitly_wait(10)
driver.maximize_window()
# 打开登录页面
driver.get('https://kyfw.12306.cn/otn/resources/login.html')

driver.find_element(By.XPATH, '//*[@id="J-userName"]').send_keys('i_wanting@yeah.net')
driver.find_element(By.XPATH, '//*[@id="J-password"]').send_keys('xiaobao168')
driver.find_element(By.XPATH, '//*[@id="J-login"]').click()
# driver.execute_script('arguments[0].click()', ele)

time.sleep(1)
ele1 = driver.find_element(By.XPATH, '//span[@class="nc_iconfont btn_slide"]')
ele2 = driver.find_element(By.XPATH, '//span[@class="nc-lang-cnt"]')
print(ele1.size)
print(ele2.size)

# 使用action滑动
action = ActionChains(driver)
# 按住滑块
action.click_and_hold(ele1)
# 拖到最右边
action.move_by_offset(ele2.size.get('width'), 0)
# 松开鼠标
action.release()
# 让这些动作执行
action.perform()

time.sleep(3)

driver.find_element(By.XPATH, '//a[text()="确定"]').click()
time.sleep(1)
action.move_to_element(driver.find_element(By.XPATH, '//a[@class="nav-hd item"]')).perform()
driver.find_element(By.XPATH, '//a[text()="单程"]').click()

driver.find_element(By.XPATH, '//*[@id="qd_closeDefaultWarningWindowDialog_id"]').click()
time.sleep(1)
